package Ep04_String.Offer_58;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/21 21:06
 * 说明：剑指offer-58 解法二：StringBuilder：双指针分别翻转 --> 整体翻转
 */

public class Mine_2 {
    public String reverseLeftWords(String s, int n) {
        if (s.length() == 1){
            return s;
        }
        StringBuilder sb = new StringBuilder(s);
        reverseString(sb, 0, n-1);
        reverseString(sb, n, s.length()-1);
        reverseString(sb, 0, s.length()-1);
        return sb.toString();
    }

    public void reverseString(StringBuilder sb, int left, int right){
        while (left < right){
            char temp = sb.charAt(left);
            sb.setCharAt(left, sb.charAt(right));
            sb.setCharAt(right, temp);
            left++;
            right--;
        }
    }
}
